package com.zoshine.zzg.util;

import org.im4java.core.*;
import org.im4java.process.ArrayListOutputConsumer;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 图片处理工具
 * @author 贾俊峰
 * @time 2017/9/7 09:20
 */
public class ImageUtil {
    /**
     * 根据坐标裁剪图片
     *
     * @param srcPath   要裁剪图片的路径
     * @param newPath   裁剪图片后的路径
     * @param x   起始横坐标
     * @param y   起始纵坐标
     * @param x1  结束横坐标
     * @param y1  结束纵坐标
     */
    public static void cutImage(String srcPath, String newPath, int x, int y, int x1,
            int y1)  throws Exception {
        int width = x1 - x;
        int height = y1 - y;
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        op.crop(width, height, x, y);
        op.addImage(newPath);
        ConvertCmd convert = new ConvertCmd(true);
        convert.run(op);
    }
    /**
     * 根据尺寸缩放图片
     * @param width  缩放后的图片宽度
     * @param height  缩放后的图片高度
     * @param srcPath   源图片路径
     * @param newPath   缩放后图片的路径
     */
    public static void zoomImage(String srcPath, String newPath, Integer width, Integer height) throws Exception {
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        if (width != null && height != null) {
            op.resize(width, height, "!");
        } else if (width != null) {//根据宽度缩放图片
            op.resize(width, null);
        } else if (height != null) {//根据高度缩放图片
            op.resize(null, height);
        }
        op.addImage(newPath);
        ConvertCmd convert = new ConvertCmd(true);
        String os = System.getProperty("os.name");
        if(os.toLowerCase().startsWith("win")) {
            convert.setSearchPath("C:/GraphicsMagick-1.3.25-Q8");
        } else {
            convert.setSearchPath("/usr/bin");
        }
        convert.run(op);
    }


    /**
     * 给图片加水印
     * @param srcPath   源图片路径
     */
    public static void addImgText(String srcPath,String content) throws Exception {
        IMOperation op = new IMOperation();
        op.font("微软雅黑");
        op.gravity("southeast");
        op.pointsize(18).fill("#BCBFC8").draw("text 0,0 "+content);   //("x1 x2 x3 x4") x1 格式，x2 x轴距离 x3 y轴距离  x4名称
        op.addImage();
        op.addImage();
        ConvertCmd convert = new ConvertCmd(true);
        try {
            convert.run(op,srcPath,srcPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 图片水印
     *
     * @param srcImagePath   源图片
     * @param waterImagePath 水印
     * @param destImagePath  生成图片
     * @param gravity  图片位置
     * @param dissolve 水印透明度
     */
    public static void waterMark(String waterImagePath, String srcImagePath, String destImagePath, String gravity, int dissolve) {
        IMOperation op = new IMOperation();
        op.gravity(gravity);
        op.dissolve(dissolve);
        op.addImage(waterImagePath);
        op.addImage(srcImagePath);
        op.addImage(destImagePath);
        CompositeCmd cmd = new CompositeCmd(true);
        try {
            cmd.run(op);
        } catch (IOException | InterruptedException | IM4JavaException e) {
            e.printStackTrace();
        }
    }
    /**
     * 图片旋转
     *
     * @param srcImagePath 图片源路径
     * @param destImagePath 目标路径
     * @param angle 放转角度
     */
    public static void rotate(String srcImagePath, String destImagePath, double angle) {
        try {
            IMOperation op = new IMOperation();
            op.rotate(angle);
            op.addImage(srcImagePath);
            op.addImage(destImagePath);
            ConvertCmd cmd = new ConvertCmd(true);
            cmd.run(op);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片信息
     *
     * @param imagePath 图片路径
     * @return 图片信息
     */
    public static String showImageInfo(String imagePath) {
        String line = null;
        try {
            IMOperation op = new IMOperation();
            op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
            op.addImage(1);
            IdentifyCmd identifyCmd = new IdentifyCmd(true);
            ArrayListOutputConsumer output = new ArrayListOutputConsumer();
            identifyCmd.setOutputConsumer(output);
            identifyCmd.run(op, imagePath);
            ArrayList<String> cmdOutput = output.getOutput();
            assert cmdOutput.size() == 1;
            line = cmdOutput.get(0);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return line;
    }
    /**
     * 图片合成
     * @param args 源图片路径（多个）
     * @param maxWidth 最大宽度
     * @param maxHeight 最大高度
     * @param newpath 新的图片路径
     * @param mrg 每行/列的图片数量
     * @param type 1:横,2:竖
     */
    public static void montage(String[] args,Integer maxWidth,Integer maxHeight,String newpath,Integer mrg,String type) {
        IMOperation op = new IMOperation();
        ConvertCmd cmd = new ConvertCmd(true);
        String thumb_size = maxWidth+"x"+maxHeight+"^";
        String extent = maxWidth+"x"+maxHeight;
        if("1".equals(type)){
            op.addRawArgs("+append");
        }else if("2".equals(type)){
            op.addRawArgs("-append");
        }

        op.addRawArgs("-thumbnail",thumb_size);
        op.addRawArgs("-gravity","center");
        op.addRawArgs("-extent",extent);

        Integer border_w = maxWidth / 40;
        op.addRawArgs("-border",border_w+"x"+border_w);
        op.addRawArgs("-bordercolor","#ccc");

        op.addRawArgs("-border",1+"x"+1);
        op.addRawArgs("-bordercolor","#fff");

        for(String img : args){
            op.addImage(img);
        }
        if("1".equals(type)){
            Integer whole_width = ((mrg / 2) +1 + border_w + maxWidth + border_w + (mrg / 2) +1)*args.length - mrg;
            Integer whole_height = maxHeight + border_w + 1;
            op.addRawArgs("-extent",whole_width + "x" +whole_height);
        }else if("2".equals(type)){
            Integer whole_width = maxWidth + border_w + 1;
            Integer whole_height = ((mrg / 2) +1 + border_w + maxHeight + border_w + (mrg / 2) +1)*args.length - mrg;
            op.addRawArgs("-extent",whole_width + "x" +whole_height);
        }
        op.addImage(newpath);
        try {
            cmd.run(op);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) throws Exception{
    //addImgText("e://a2.jpg");
//        zoomImage("c:/users/abel/desktop/1.jpg", "c:/users/abel/desktop/2.jpg", 300, 150);
    //waterMark("e://cc.jpg", "e://aa.jpg", "e://bb.jpg", "southeast", 30);
    //rotate("e://aa.jpg", "e://ee.jpg", 90);
    //System.out.println(showImageInfo("e://aa.jpg"));
//        String[] files = new String[5];
//        files[0] = "e://a1.jpg";
//        files[1] = "e://b1.jpg";
//        files[2] = "e://c1.jpg";
//        files[3] = "e://d1.jpg";
//        files[4] = "e://e1.jpg";
    //montage(files, 280, 200, "e://liboy1.jpg", 0,"2");
    //cropImage("e://a.jpg", "e://liboy22.jpg", 1024, 727, 500, 350);
//        cutImage("e://a.jpg", "e://liboy222.jpg", 5, 10, 100, 120);
//    }
}
